﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataProcessor.EF;
using Hjt212Parser;
using log4net;


namespace DataProcessor.Process
{
    class Util
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(Util));
        private static List<ps_pi_port> _enterpriseInfoBuffer;
        /// <summary>
        /// 根据Mn获取排口信息
        /// </summary>
        /// <param name="mn"></param>
        /// <returns></returns>
        public static ps_pi_port GetInfoFromMn(string mn)
        {
            try
            {
                if (_enterpriseInfoBuffer == null || MyTimer.IsToTheTime("Util.GetInfoFromMn", 10))
                {
                    using (var ctx = new PlatformVocContext())
                    {
                        _enterpriseInfoBuffer = ctx.ps_pi_port.AsNoTracking().ToList();
                    }
                }
                return _enterpriseInfoBuffer.FirstOrDefault(t => t.mn.Equals(mn));

            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            return null;
        }

        private static List<sc_pollutant_factor> _contaminants;
        /// <summary>
        /// 根据代码返回污染物因子对象
        /// </summary>
        /// <param name="code">接收串中的污染物因子代码</param>
        /// <returns></returns>
        public static sc_pollutant_factor GetFactor(string code)
        {
            try
            {
                if (_contaminants == null || MyTimer.IsToTheTime("Util.GetFactor", 10))
                {
                    using (var ctx = new PlatformVocContext())
                    {
                        _contaminants = ctx.sc_pollutant_factor.AsNoTracking().ToList();
                    }
                }
                var result = _contaminants.FirstOrDefault(t => t.old_code.Equals(code));
                if (result == null)
                {
                    result = _contaminants.FirstOrDefault(t => t.code.Equals(code));
                }
                return result;

            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            return null;
        }

        public static DateTime OffsetMinute(DateTime time, int minute)
        {
            return new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, 0).AddMinutes(minute);
        }
        public static DateTime TrimToHour(DateTime time, int hours)
        {
            return new DateTime(time.Year, time.Month, time.Day, time.Hour, 0, 0).AddHours(hours);
        }
        public static DateTime TrimToDay(DateTime time, int days)
        {
            return new DateTime(time.Year, time.Month, time.Day).AddDays(days);
        }
        public static DateTime TrimToMonth(DateTime time, int months)
        {
            return new DateTime(time.Year, time.Month, 1).AddMonths(months);
        }
        public static DateTime TrimToYear(DateTime time, int year)
        {
            return new DateTime(time.Year, 1, 1).AddYears(year);
        }
     
    }
}
